import React, { useEffect, useState } from 'react';
import { useRequest } from 'ahooks';
import styles from './index.module.less';
import { corsRequest } from '../../../request';
import { EstimateApiRes } from '../../../type';
import axios from 'axios';
const useEstimate = (code: string) => {
  //基金基础信息
  const [name, setName] = useState('');

  //预估信息
  const [estimateObj, setEstimateObj] = useState({
    date: '',
    val: 0,
    percent: 0,
  });

  //获取当前预估值
  const getEstimateVal = async () => {
    try {
      const { Expansion, Datas }: EstimateApiRes = await corsRequest.get(
        `https://tiantian-fund-api.vercel.app/api/action?action_name=fundVarietieValuationDetail&FCODE=${code}`,
      );
      const [_, date, per] = Datas?.pop?.()?.split?.(',') ?? [];
      const curEstPer = Number(per);
      const curEstNum = (Number(Expansion?.DWJZ ?? 0) * (1 + curEstPer / 100)).toFixed(4);

      setName(Expansion?.SHORTNAME);
      setEstimateObj({
        val: Number(curEstNum ?? 0),
        date,
        percent: curEstPer,
      });
    } catch (e) {
      console.log('getEstimateValError', e);
    }
  };

  

  useEffect(() => {
    getEstimateVal();
  }, [code]);
  return {
    name,
    estimateObj,
  };
};
export default useEstimate;
